package com.ivo.activiti.bpmn.record;

import com.ivo.activiti.hr.entity.Department;
import com.ivo.activiti.hr.entity.Employee;
import com.ivo.activiti.hr.entity.Position;
import com.ivo.activiti.hr.service.HrService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wj
 * @version 1.0
 */
@Service
public class AssigneeService {

    @Resource
    private AssigneeRepository assigneeRepository;

    @Resource
    private HrService hrService;

    List<Assignee> getAssignee(String node) {
        return assigneeRepository.findByNode(node);
    }

    Assignee getAssignee(Long id) {
        return assigneeRepository.findById(id).orElse(null);
    }

    void saveAssignee(Assignee assignee) {
        assigneeRepository.save(assignee);
    }

    void delAssignee(long id) {
        Assignee assignee = assigneeRepository.findById(id).orElse(null);
        if(assignee != null)
            assigneeRepository.delete(assignee);
    }

    List<Actor> getActor(String node) {
        List<Assignee> assigneeList = getAssignee(node);
        List<Actor> actorList = new ArrayList<>();
        for(Assignee assignee : assigneeList) {
            switch (Objects.requireNonNull(AssigneeType.getByType(assignee.getType()))) {
                case Position :
                    actorList = getActorByPosition(assignee.getParam());
                    break;
                case Department :
                    actorList = getActorByDepartment(assignee.getParam());
                    break;
                case Employee :
                    actorList = getActorByEmployee(assignee.getParam());
                    break;
                case Param :
                    actorList = getActorByParam(assignee.getParam());
                    break;
                default:
            }
        }
        return actorList;
    }

    List<Actor> getActorByPosition(String positionId) {
        Position position = hrService.getPosition(positionId);
        List<Employee> employeeList = hrService.getEmpByPosition(positionId);
        Department department = position.getDepartment();
        List<Actor> actorList = new ArrayList<>();
        for(Employee employee : employeeList) {
            Actor actor = new Actor(department, employee);
            actorList.add(actor);
        }
        return actorList;
    }

    List<Actor> getActorByDepartment(String deptId) {
        Department department = hrService.getDept(deptId);
        Employee employee = department.getDeptHead();
        Actor actor = new Actor(department, employee);
        List<Actor> actorList = new ArrayList<>();
        actorList.add(actor);
        return actorList;
    }

    List<Actor> getActorByEmployee(String empId) {
        Employee employee = hrService.getEmp(empId);
        Department department = employee.getDepartment();
        Actor actor = new Actor(department, employee);
        List<Actor> actorList = new ArrayList<>();
        actorList.add(actor);
        return actorList;
    }

    List<Actor> getActorByParam(String param) {
        List<Actor> actorList = new ArrayList<>();
        return actorList;
    }
}
